<?php
namespace S\Util;

class Curl {

    protected $url;
    protected $ch;
    public $lastInfo;
    protected $lastResult;

    public function __construct($url = null) {
        $this->url = $url;
        $this->ch = curl_init($url);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    }

    public function setopt($type, $val) {
        curl_setopt($this->ch, $type, $val);
    }

    public function get($fields = null) {
        curl_setopt($this->ch, CURLOPT_POST, 0);
        return $this->exec($fields);
    }

    public function post($fields = null) {
        curl_setopt($this->ch, CURLOPT_POST, 1);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, []);
        return $this->exec($fields);
    }

    public function exec($fields = null) {
        if ($fields) {
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $fields);
        }
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        $this->lastResult = curl_exec($this->ch);
        $this->lastInfo = curl_getinfo($this->ch);
        curl_close($this->ch);
        return $this->lastResult;
    }
}
